﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shop4Aggies.Data_Access;

namespace Shop4Aggies
{
    public partial class ProductsList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Query"]))
            {
                string query = Request.QueryString["Query"];

                string[] queryDiv = HttpUtility.UrlDecode(query).Split(new char[]{' '});

                using (CommerceEntities db = new CommerceEntities())
                {
                    var q =  from product in db.Products
                             where
                             (queryDiv.Any(s => product.ModelName.Contains(s) || product.Description.Contains(s)))
                             select new
                             {
                                 ModelName = product.ModelName,
                                 ProductID = product.ProductID,
                                 Description = product.Description,
                                 ProductImage = product.ProductImage,
                                 UnitCost = product.UnitCost
                             };

                    ListView_Products.DataSourceID = "";
                    ListView_Products.DataSource = q;
                    ListView_Products.DataBind();
                }
            }
        }
    }
}